<?php
/**
 * +----------------------------------------------------------------------
 * | TpMeCMS 
 * +----------------------------------------------------------------------
 *                      .::::.
 *                    .::::::::.            | AUTHOR: Xiaohe
 *                    :::::::::::           | EMAIL: 496631085@qq.com
 *                 ..:::::::::::'           | QQ: 496631085
 *             '::::::::::::'               | WECHAT: he4966
 *                .::::::::::               | DATETIME: 2021/06/21
 *           '::::::::::::::..
 *                ..::::::::::::.
 *              ``::::::::::::::::
 *               ::::``:::::::::'        .:::.
 *              ::::'   ':::::'       .::::::::.
 *            .::::'      ::::     .:::::::'::::.
 *           .:::'       :::::  .:::::::::' ':::::.
 *          .::'        :::::.:::::::::'      ':::::.
 *         .::'         ::::::::::::::'         ``::::.
 *     ...:::           ::::::::::::'              ``::.
 *   ```` ':.          ':::::::::'                  ::::..
 *                      '.:::::'                    ':'````..
 * +----------------------------------------------------------------------
 */

namespace app\cms\controller;

// use app\common\controller\Frontend;
use app\cms\controller\Cms;
use think\Db;

class Com extends Cms
{

    protected $noNeedLogin = ['*'];//自己需要写的方法 这个是不需要登陆的其他则为需要登陆
    protected $noNeedRight = '*';



    public function _initialize()
    {   
        parent::_initialize();       
    }

    /**公共方法  更多通用方法写在里面
     *  ┏┻━━━━━━━━━┻┓
     *  ┃           ┃
     *  ┃ ┗┳     ┳┛ ┃
     *  ┃     ┻     ┃
     *  ┗━━━┓　┏━━━━┛
     *      ┃　┃
     *      ┃　┃
     *      ┃　┗━━━━━━━━━━┓
     *      ┃     He      ┣┓
     *      ┃　          ┏┛
     *      ┗━┓  ┏━━━┓  ┏┛
     *        ┗━━┛   ┗━━┛
     */



    
    /**
     * 
     * 更新用户信息
     * 
     * @ApiInternal()
     */
    public function update_user_data($data,$sharer=null)
    {
        $user = Db::name('user')->where('wx_openid',$data['openid'])->find();
        
        $in_data['logintime'] = time();//登陆时间
        if($user){
            $in_data['sex'] = $data['sex'];
            $in_data['city'] = $data['city'];
            $in_data['avatar'] = $data['headimgurl'];
            $in_data['nickname'] = $data['nickname'];
            if($sharer && empty($user['pid'])){
                $in_data['pid'] = base64_decode($sharer);
               
                
            }
            
            Db::name('user')->where('wx_openid',$data['openid'])->update($in_data);
            //修改数据；
        }else{
            
            //分享者
            if($sharer){
                $in_data['pid'] = base64_decode($sharer);
                
                

            }
            $in_data['wx_openid'] = $data['openid'];
            $in_data['sex'] = $data['sex'];
            $in_data['city'] = $data['city'];
            $in_data['avatar'] = $data['headimgurl'];
            $in_data['nickname'] = $data['nickname'];
            $in_data['status'] = 'normal';
            $new_uid = Db::name('user')->insertGetId($in_data);
            //插入数据
       
        }
    }





    /**
     * 获取用户的openid
     * @ApiInternal()
     *
     * @param array $staffs
     * @return void
     */
    public function get_user_openids($uids = null)
    {
        if(is_array($uids)){
            $list = Db::name('user')->where('id','in',$uids)->column('wx_openid');
        }else{
            $list = Db::name('user')->where('id',$uids)->value('wx_openid');
        }

        return $list;
    }



    /**
     * openid获取用户信息
     * @ApiInternal()
     * @param string $openid
     * @return void
     */
    public function openid_get_user($openid)
    {
       $user =  Db::name('user')->where('wx_openid',$openid)->find();
       return $user;
    }
    

}
